package com.ruoyi.business.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ruoyi.business.domain.Bank;
import com.ruoyi.business.domain.vo.BankVO;
import com.ruoyi.business.mapper.BankMapper;
import com.ruoyi.business.service.BankService;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.StringUtils;

import java.util.Map;

import static com.ruoyi.business.domain.table.BankTableDef.BANK;


/**
 * 服务实现 : 银行信息
 *
 * @Author : ljq
 * @Created : 2025/10/4
 */
@Service
public class BankServiceImpl extends ServiceImpl<BankMapper, Bank> implements BankService {

    @Override
    public Page<BankVO> listByCondition(Bank entity, PageDomain domain) {
        QueryChain<Bank> queryChain = QueryChain.of(this.getMapper())
                .select(BANK.DEFAULT_COLUMNS)
                .from(BANK).as("t1");
        this.baseCondition(queryChain, entity);
        return queryChain
                .withRelations()
                .pageAs(new Page<>(domain.getPageNum(), domain.getPageSize()), BankVO.class);
    }


    /* ------------------- 基础查询条件 --------------------- */
    private QueryChain baseCondition(QueryChain chain, Bank entity) {
        // 银行代码
        if (StringUtils.isNotEmpty(entity.getBankCode())) {
            chain.where(BANK.BANK_CODE.eq(entity.getBankCode()));
        }
        // 银行名称
        if (StringUtils.isNotEmpty(entity.getBankName())) {
            chain.where(BANK.BANK_NAME.eq(entity.getBankName()));
        }
        // 银行简称
        if (StringUtils.isNotEmpty(entity.getBankShortName())) {
            chain.where(BANK.BANK_SHORT_NAME.eq(entity.getBankShortName()));
        }
        // 银行logo URL
        if (StringUtils.isNotEmpty(entity.getBankLogo())) {
            chain.where(BANK.BANK_LOGO.eq(entity.getBankLogo()));
        }
        // 客服电话
        if (StringUtils.isNotEmpty(entity.getCustomerServicePhone())) {
            chain.where(BANK.CUSTOMER_SERVICE_PHONE.eq(entity.getCustomerServicePhone()));
        }
        // 官方网站
        if (StringUtils.isNotEmpty(entity.getWebsite())) {
            chain.where(BANK.WEBSITE.eq(entity.getWebsite()));
        }
        // 状态
        if (StringUtils.isNotEmpty(entity.getStatus())) {
            chain.where(BANK.STATUS.eq(entity.getStatus()));
        }

        Map<String, Object> params = entity.getParams();

        // 创建时间
        String createTimeRange = params.getOrDefault("createTimeRange", "").toString();
        if (StringUtils.isNotEmpty(createTimeRange)) {
            String[] split = createTimeRange.split(",");
            chain.where(BANK.CREATE_TIME.between(split[0], split[1]));
        }

        // 修改时间
        String updateTimeRange = params.getOrDefault("updateTimeRange", "").toString();
        if (StringUtils.isNotEmpty(updateTimeRange)) {
            String[] split = updateTimeRange.split(",");
            chain.where(BANK.UPDATE_TIME.between(split[0], split[1]));
        }

        // 银行代码-模糊左匹配
        String bankCodeLikeLeft = params.getOrDefault("bankCodeLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(bankCodeLikeLeft)) {
            chain.where(BANK.BANK_CODE.likeLeft(bankCodeLikeLeft));
        }

        // 银行名称-模糊左匹配
        String bankNameLikeLeft = params.getOrDefault("bankNameLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(bankNameLikeLeft)) {
            chain.where(BANK.BANK_NAME.likeLeft(bankNameLikeLeft));
        }

        // 银行简称-模糊左匹配
        String bankShortNameLikeLeft = params.getOrDefault("bankShortNameLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(bankShortNameLikeLeft)) {
            chain.where(BANK.BANK_SHORT_NAME.likeLeft(bankShortNameLikeLeft));
        }

        // 银行logo URL-模糊左匹配
        String bankLogoLikeLeft = params.getOrDefault("bankLogoLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(bankLogoLikeLeft)) {
            chain.where(BANK.BANK_LOGO.likeLeft(bankLogoLikeLeft));
        }

        // 客服电话-模糊左匹配
        String customerServicePhoneLikeLeft = params.getOrDefault("customerServicePhoneLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(customerServicePhoneLikeLeft)) {
            chain.where(BANK.CUSTOMER_SERVICE_PHONE.likeLeft(customerServicePhoneLikeLeft));
        }

        // 官方网站-模糊左匹配
        String websiteLikeLeft = params.getOrDefault("websiteLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(websiteLikeLeft)) {
            chain.where(BANK.WEBSITE.likeLeft(websiteLikeLeft));
        }

        // 状态-模糊左匹配
        String statusLikeLeft = params.getOrDefault("statusLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(statusLikeLeft)) {
            chain.where(BANK.STATUS.likeLeft(statusLikeLeft));
        }

        // 创建者-模糊左匹配
        String createByLikeLeft = params.getOrDefault("createByLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(createByLikeLeft)) {
            chain.where(BANK.CREATE_BY.likeLeft(createByLikeLeft));
        }

        // 修改者-模糊左匹配
        String updateByLikeLeft = params.getOrDefault("updateByLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(updateByLikeLeft)) {
            chain.where(BANK.UPDATE_BY.likeLeft(updateByLikeLeft));
        }

        // 银行代码-模糊匹配
        String bankCodeLike = params.getOrDefault("bankCodeLike", "").toString();
        if (StringUtils.isNotEmpty(bankCodeLike)) {
            chain.where(BANK.BANK_CODE.like(bankCodeLike));
        }

        // 银行名称-模糊匹配
        String bankNameLike = params.getOrDefault("bankNameLike", "").toString();
        if (StringUtils.isNotEmpty(bankNameLike)) {
            chain.where(BANK.BANK_NAME.like(bankNameLike));
        }

        // 银行简称-模糊匹配
        String bankShortNameLike = params.getOrDefault("bankShortNameLike", "").toString();
        if (StringUtils.isNotEmpty(bankShortNameLike)) {
            chain.where(BANK.BANK_SHORT_NAME.like(bankShortNameLike));
        }

        // 银行logo URL-模糊匹配
        String bankLogoLike = params.getOrDefault("bankLogoLike", "").toString();
        if (StringUtils.isNotEmpty(bankLogoLike)) {
            chain.where(BANK.BANK_LOGO.like(bankLogoLike));
        }

        // 客服电话-模糊匹配
        String customerServicePhoneLike = params.getOrDefault("customerServicePhoneLike", "").toString();
        if (StringUtils.isNotEmpty(customerServicePhoneLike)) {
            chain.where(BANK.CUSTOMER_SERVICE_PHONE.like(customerServicePhoneLike));
        }

        // 官方网站-模糊匹配
        String websiteLike = params.getOrDefault("websiteLike", "").toString();
        if (StringUtils.isNotEmpty(websiteLike)) {
            chain.where(BANK.WEBSITE.like(websiteLike));
        }

        // 状态-模糊匹配
        String statusLike = params.getOrDefault("statusLike", "").toString();
        if (StringUtils.isNotEmpty(statusLike)) {
            chain.where(BANK.STATUS.like(statusLike));
        }

        // 创建者-模糊匹配
        String createByLike = params.getOrDefault("createByLike", "").toString();
        if (StringUtils.isNotEmpty(createByLike)) {
            chain.where(BANK.CREATE_BY.like(createByLike));
        }

        // 修改者-模糊匹配
        String updateByLike = params.getOrDefault("updateByLike", "").toString();
        if (StringUtils.isNotEmpty(updateByLike)) {
            chain.where(BANK.UPDATE_BY.like(updateByLike));
        }

        return chain;
    }
}